package com.lkwlw.tplms.order.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import com.lkwlw.tplms.model.Pager;
import com.lkwlw.tplms.order.dao.mapper.OrderinfoMapper;
import com.lkwlw.tplms.order.dao.model.Orderinfo;
import com.lkwlw.tplms.order.dao.model.OrderinfoExample;
import com.lkwlw.tplms.order.dao.model.OrderinfoExample.Criteria;
import com.lkwlw.tplms.system.dao.model.Admininfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PlaceOrderAction extends ActionSupport {
	private static final long serialVersionUID = 821473979801719716L;

	private Orderinfo order;	
	private OrderinfoMapper orderInfoDAO;
	private List<Orderinfo> orderList;
	private String selectIds;	
	private Pager pager;	
	private int page = 1;
	
	public String execute() throws Exception {
		OrderinfoExample example = new OrderinfoExample();
		if(null != order){
			Criteria criteria =  example.createCriteria();			
			if(null != order.getSender() && !"".equals(order.getSender())){
				criteria.andSenderLike("%" + order.getSender() + "%");
			}
			if(null != order.getSendertel() && !"".equals(order.getSendertel())){
				criteria.andSendertelLike("%" + order.getSendertel() + "%");
			}
			if(null != order.getState() && !"".equals(order.getState())){
				criteria.andStateEqualTo(order.getState());
			}
		}		
		orderList = orderInfoDAO.selectByExample(null,example);
		example.setOrderByClause("sender asc");
		getPagination(example);
		return SUCCESS;
	}
	
	public String save() throws Exception{
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();
		if (session.get("admin") != null) {
			Admininfo admin = (Admininfo) session.get("admin");
			order.setRegistrant(admin.getFullName());
		}
		if (null == order.getId() || "".equals(order.getId())) {
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddmmss");
			order.setId(format.format(new Date().getTime()));
			orderInfoDAO.insertSelective(order);
		}else{
			orderInfoDAO.updateByPrimaryKeySelective(order);
		}		
		order = null;
		execute();
		return SUCCESS;
	}
	
	public String edit() throws Exception {
		if (null != selectIds) {
			order = orderInfoDAO.selectByPrimaryKey(selectIds);
		}		
		execute();
		return SUCCESS;
	}
	
	public String delete() throws Exception {
		if (null != selectIds && !"".equals(selectIds)) {
			if (-1 < selectIds.indexOf(",")) {
				String[] deleteIds = selectIds.split(",");		
				orderInfoDAO.deleteByPrimaryKeys(deleteIds);
			}else {
				orderInfoDAO.deleteByPrimaryKey(selectIds);
			}				
		}		
		order = null;
		execute();
		return SUCCESS;
	}
	public String receive() throws Exception{
		if (null != selectIds && !"".equals(selectIds)) {
			order = orderInfoDAO.selectByPrimaryKey(selectIds);			
			if(order.getState().equals("未收件")){
				return SUCCESS;
			}
		}
		//order = null;
		execute();
		return ERROR;
	}
	public String cancelOrder() throws Exception {
		if (null != selectIds && !"".equals(selectIds)) {
			String[] cancelId = selectIds.split(",");
			for (int i = 0; i < cancelId.length; i++) {
				order = orderInfoDAO.selectByPrimaryKey(cancelId[i]);
				if(order.getState().equals("未收件")){
					order.setState("已取消");
					orderInfoDAO.updateStateByPrimaryKey(order);
				}			
			}
		}		
		order = null;
		execute();
		return SUCCESS;
	}	
	
	private void getPagination(OrderinfoExample example){
		int pageSize=10;
		pager.setCurrentPage(page);
		pager.setPageSize(pageSize);
		
		// 获得总记录数
		int totalSize = orderInfoDAO.countByExample(example);
		pager.setTotalCount(totalSize);
		Map<String, Integer> limitParam = new HashMap<String, Integer>();
		limitParam.put("offset", pager.getStart());
		limitParam.put("rows", pager.getPageSize());
		
		orderList = null;		
		orderList = orderInfoDAO.selectByExample(limitParam,example);
		pager.setItems(orderList);
		pager.setPaginationModel(getPagerStr(totalSize));		
	}
	
	/**
	 * 组织分页导航栏
	 * @return
	 */
	public String  getPagerStr(int totalSize) {
		
		StringBuilder sb=new StringBuilder();
		
		int pageCount=pager.getTotalPage();//总页数
		int a_total=10;      //分页条中有多少个超链接
		
		sb.append("<table><tr><td align='center'class='wordblue'>共" + pager.getTotalCount()+"条数据&nbsp;&nbsp;页次");
		sb.append(pager.getCurrentPage()+"/" + (totalSize == 0 ? 1 : pageCount) + "页&nbsp;&nbsp;</td>");
		//实际应用中修改上面第三个参数即可

		int a_padding=(int)Math.ceil(a_total/2);  //中间的那个超链接距离边缘链接的间隔a的个数 例如：共11个分页 那么这个就是5

		if(pageCount - pager.getCurrentPage() <= a_padding && pager.getCurrentPage() > a_padding + 1) {
			a_padding=a_total-(pageCount-pager.getCurrentPage());
		}

		int start=pager.getCurrentPage()-a_padding,
		end=start+a_total;
		

		if(pager.getCurrentPage() > 1){
			sb.append("<td class='wordblue'><a href=order/placeOrder.action?page=1>首页</a>&nbsp;&nbsp;</td>");
			sb.append("<td ><div class='prepage wordblue'><a href=order/placeOrder.action?page="+(pager.getCurrentPage()-1)+">上一页</a></div></td>");
		}
		sb.append("<td ><div class='pagelist'>");
		if (totalSize > 0) {
			for (int i = start; i <=end; i++) {
				if(i<=0){
					end+=Math.abs(i);
					i=1;	
				}
				sb.append(" <a href=order/placeOrder.action?page=" + i + " " + ( i == pager.getCurrentPage() ? "style=\"color:red;\"":"") + "> " + i + " </a>");	
				if(i==pageCount){
					break;
				}
			}
		}		
		sb.append("</div></td>");
		if(pager.getCurrentPage()-pageCount<0){
			sb.append("<td><div class='nextpage wordblue'>");
			sb.append("<a href=order/placeOrder.action?page=" + ( pager.getCurrentPage() + 1 ) + ">下一页</a></div></td>");
			sb.append("<td><div class='wordblue'><a href=order/placeOrder.action?page=" + pageCount + ">尾页</a></div></td>");
			}
		sb.append("</tr></table>");
		return sb.toString();
	}
	
	public Orderinfo getOrder() {
		return order;
	}

	public void setOrder(Orderinfo order) {
		this.order = order;
	}

	public List<Orderinfo> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<Orderinfo> orderList) {
		this.orderList = orderList;
	}

	public String getSelectIds() {
		return selectIds;
	}

	public void setSelectIds(String selectIds) {
		this.selectIds = selectIds;
	}

	public void setOrderInfoDAO(OrderinfoMapper orderInfoDAO) {
		this.orderInfoDAO = orderInfoDAO;
	}
	
	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

}
